package a66weding.com.jiehuntong.common.utils.upfile;

import android.util.Log;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

/**
 * Created by admin on 2016/7/19.
 */
public class XUtil {
    /**
     * 上传文件
     * @param <T>
     */
    public static <T> Callback.Cancelable UpLoadFile(String url, Map<String,Object> map, Callback.CommonCallback<T> callback){
        RequestParams params=new RequestParams(url);
        if(null!=map){
            for(Map.Entry<String, Object> entry : map.entrySet()){
                params.addParameter(entry.getKey(), entry.getValue());
            }
        }
        params.setMultipart(true);
        Callback.Cancelable cancelable = x.http().post(params, callback);
        return cancelable;
    }

    public static byte[] getByte(String fileName) {
        byte[] bt = null;
        FileInputStream stream = null;
        ByteArrayOutputStream out = null;
        try {
            stream = new FileInputStream(fileName);
            out = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int len = 0;
            while ((len = stream.read(b)) != -1) {
                out.write(b, 0, len);
            }
            stream.close();
            out.close();
            bt = out.toByteArray();
            Log.i("byteArray", ""+bt);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (stream != null)
                    stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                if (out != null)
                    out.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        out = null;
        return bt;
    }


}
